//
//  FunPlayView.swift
//  FunReader
//
//  Created by yht on 2025/1/22.
//

import UIKit
import AVFoundation

// 1. 定义协议
protocol FunPlayViewDelegate: AnyObject {
    func didSelectedImage(image: UIImage)
    func didRead();
}

class FunPlayView: UIView {
    weak var delegate : FunPlayViewDelegate?
    
    let imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.backgroundColor = .lightGray
        return imageView
    }()
    
    lazy var takePhotoButton: UIButton = {
        let button = UIButton(type:.system)
        button.setTitle("Take Photo", for:.normal)
        button.addTarget(self, action: #selector(takePhotoButtonTapped), for:.touchUpInside)
        return button
    }()
    
    lazy var selectPhotoButton: UIButton = {
        let button = UIButton(type:.system)
        button.setTitle("Select Photo", for:.normal)
        button.addTarget(self, action: #selector(selectPhotoButtonTapped), for:.touchUpInside)
        return button
    }()
    
    lazy var readButton: UIButton = {
        let button = UIButton(type:.system)
        button.setTitle("Read", for:.normal)
        button.addTarget(self, action: #selector(readButtonTapped), for:.touchUpInside)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }
    
    private func setupView() {
        backgroundColor = UIColor.white
        addSubview(imageView)
        addSubview(takePhotoButton)
        addSubview(selectPhotoButton)
        addSubview(readButton)
        
        imageView.translatesAutoresizingMaskIntoConstraints = false
        takePhotoButton.translatesAutoresizingMaskIntoConstraints = false
        selectPhotoButton.translatesAutoresizingMaskIntoConstraints = false
        readButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 设置 imageView 的约束
        NSLayoutConstraint.activate([
            imageView.topAnchor.constraint(equalTo: topAnchor),
            imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
            imageView.trailingAnchor.constraint(equalTo: trailingAnchor),
            imageView.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.8)
        ])
        
        // 设置 takePhotoButton 的约束
        NSLayoutConstraint.activate([
            takePhotoButton.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 10),
            takePhotoButton.centerXAnchor.constraint(equalTo: centerXAnchor)
        ])
        
        // 设置 selectPhotoButton 的约束
        NSLayoutConstraint.activate([
            selectPhotoButton.topAnchor.constraint(equalTo: takePhotoButton.bottomAnchor, constant: 10),
            selectPhotoButton.centerXAnchor.constraint(equalTo: centerXAnchor)
        ])
        
        // 设置 selectPhotoButton 的约束
        NSLayoutConstraint.activate([
            readButton.topAnchor.constraint(equalTo: selectPhotoButton.bottomAnchor, constant: 10),
            readButton.centerXAnchor.constraint(equalTo: centerXAnchor)
        ])
    }
    @objc func takePhotoButtonTapped() {
        let imagePicker = UIImagePickerController()
        imagePicker.sourceType = .camera
        imagePicker.delegate = self
        // 获取当前的 UIWindowScene
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
           let rootViewController = windowScene.windows.first?.rootViewController {
            rootViewController.present(imagePicker, animated: true, completion: nil)
        }
    }
    
    @objc func selectPhotoButtonTapped() {
        let imagePicker = UIImagePickerController()
        imagePicker.sourceType = .photoLibrary
        imagePicker.delegate = self
        // 获取当前的 UIWindowScene
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
           let rootViewController = windowScene.windows.first?.rootViewController {
            rootViewController.present(imagePicker, animated: true, completion: nil)
        }
    }
    
    @objc func readButtonTapped() {
        self.delegate?.didRead();
    }
    
}

extension FunPlayView: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            imageView.image = pickedImage
            self.delegate?.didSelectedImage(image: pickedImage)
        }
        picker.dismiss(animated: true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
}
